package wc.assignmentKeys;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import wc.fileUtil.FileCollector;
import wc.fileUtil.FileReader;
import wc.parserUtil.ParserWords;

public class RIDF {

	private IDF _idf;
	private int _countFiles;
	
	public RIDF(FileCollector fileCollector, FileReader fileReader, ParserWords parserWords) {
		_countFiles = fileCollector.getFileCount();
		_idf = new IDF(fileCollector, fileReader, parserWords);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map.Entry<String, Float>> getRIDF() {
		HashMap<String, Float> unsortRIDF = new HashMap<String, Float>();
		for(String key : _idf.getFrequency().keySet()) {
			unsortRIDF.put(key, (float) Math.log(_countFiles / _idf.getFrequency().get(key) - 1));
		}
		
		List<Map.Entry<String, Float>> ridf = new ArrayList<Map.Entry<String, Float>>(unsortRIDF.entrySet());
        Collections.sort(ridf, new Comparator<Object>() {
            public int compare(Object o, Object o1) {
				Map.Entry<String, Float> e1 = (Map.Entry<String, Float>)o;
                Map.Entry<String, Float> e2 = (Map.Entry<String, Float>)o1;
                Float first = (Float)e1.getValue();
                Float second = (Float)e2.getValue();
                if(first > second) {
                	return -1;
                } else if(first < second) {
                	return 1;
                } else {
                	return -e1.getKey().toLowerCase().compareTo(e2.getKey().toLowerCase());
                }
            }
        });
		
		return ridf;
	}
	
}
